package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.domain.User;
import cn.edu.sdjzu.xg.bysj.security.Actor;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;
import cn.edu.sdjzu.xg.bysj.security.UserDao;
import cn.edu.sdjzu.xg.bysj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class UserService {
    private static final UserService userService = new UserService();
    private final UserDao userDao = UserDao.getInstance();

    public UserService() {
    }

    public static UserService getInstance() {
        return UserService.userService;
    }

    public Collection<User> getUsers() throws SQLException {
        return userDao.findAll();
	}
	
	public User find(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        User user = userDao.find(connection, id);
        JdbcHelper.close(connection);
        return user;
	}
	
//	public boolean updateUser(User user)throws SQLException {
//		userDao.delete(user);
//		return userDao.add(user);
//	}
//
//	public boolean addUser(User user)throws SQLException {
//		return userDao.add(user);
//	}
//
//    public boolean deleteUser(Integer id) throws SQLException, BysjException {
//        User user = this.find(id);
//        return this.deleteUser(user);
//    }
//
//    public boolean deleteUser(User user) throws SQLException {
//        return userDao.delete(user);
//    }

    public Actor login(User userToLogin) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        Actor actor = userDao.login(connection, userToLogin);
        JdbcHelper.close(connection);
        return actor;
    }
}
